package com.hns.can.energy.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.DeptEnergyService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 部门能耗
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */

@Controller
@RequestMapping("/deptEnergy")
public class DeptEnergyController extends BaseController{
    
    @Autowired
    DeptEnergyService deptEnergyService;
    @Autowired
    private CommonInitParamService commonInitParamService;
    
    final String[] headerNames={"营运线路","所属机构","运营日期","运行时长","运行里程","燃料类型","运行能耗","百公里能耗","标准能耗","能耗百分比","线路平均能耗","是否节能"};
    final String[] headerFields={"lineName","organName","days","rngOprtDuraTr","rngMile","fuelTypeName","rngEnerCons","enerConsMile","refrEnerCons","enerConsPers","lineEnerConsMile","isSaveEner"};
    
    /**
     * 部门能耗查询
     * @param vo
     * @param response
     * @param request
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/searchDeptEnergy")
    @ResponseBody
    public Object searchDeptEnergy(NewEnergy vo, HttpServletResponse response, HttpServletRequest request) throws Exception {
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        
        Pagination page = deptEnergyService.searchDeptEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 部门能耗导出excel                
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportDeptEnergyExcel")
    public void exportDeptEnergyExcel(NewEnergy vo,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysUserEntity user = (SysUserEntity)request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = deptEnergyService.exportDeptEnergy(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("部门能耗明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }
    
    /**
     * 部门能耗导出Pdf
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportDeptEnergyPdf")
    public void exportDeptEnergyPdf(NewEnergy vo,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysUserEntity user = (SysUserEntity)request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = deptEnergyService.exportDeptEnergy(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("部门能耗明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        
        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
    /**
     * 部门能耗查询
     * @param vo
     * @param response
     * @param request
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/getEnergyDetail")
    @ResponseBody
    public Object searchEnergyDetail(NewEnergy vo, HttpServletResponse response, HttpServletRequest request) throws Exception {
    	commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        Pagination page = deptEnergyService.searchDeptEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
}
